package com.hk.yajin.function.index;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.hk.developmentlib.utills.FastjsonUtils;
import com.hk.developmentlib.utills.UiUtil;
import com.hk.yajin.R;
import com.hk.yajin.base.MyApplication;
import com.hk.yajin.function.news.ArticleDetailActivity;
import com.hk.yajin.function.news.NewsListQuickAdapter;
import com.hk.yajin.function.videos.activity.VideoDetailActivity;
import com.hk.yajin.function.videos.activity.ViewPorintDetailActivity;
import com.hk.yajin.function.videos.adapter.ViewPorintMultQuickAdapter;
import com.hk.yajin.model.IndexModel;
import com.hk.yajin.model.Message;
import com.hk.yajin.model.News;
import com.hk.yajin.model.Video;
import com.hk.yajin.model.ViewPorint;
import com.jude.rollviewpager.OnItemClickListener;
import com.jude.rollviewpager.RollPagerView;
import com.jude.rollviewpager.hintview.ColorPointHintView;

import java.util.List;

import static com.hk.yajin.base.BaseFragment.DATA_STRING;
import static com.hk.yajin.base.BaseFragment.VIDEO;

/**
 * https://github.com/CymChad/BaseRecyclerViewAdapterHelper
 */
public class IndexMultQuickAdapter extends BaseMultiItemQuickAdapter<IndexModel, BaseViewHolder> {

    public final static int TYPE_MESSAGE = 0;
    public final static int TYPE_NEWS = 1;
    public final static int TYPE_LUNBO = 2;
    public final static int TYPE_VIEW = 3;

    private Context context;

    public IndexMultQuickAdapter(List<IndexModel> datas, Context ct) {
        super(datas);
        context = ct;
        addItemType(0, R.layout.list_item_recyclerciew);
        addItemType(1, R.layout.list_item_recyclerciew);
        addItemType(2, R.layout.list_head_news);
        addItemType(3, R.layout.list_item_recyclerciew);
    }

    @Override
    protected void convert(BaseViewHolder helper, IndexModel model) {

        switch (model.getItemType()) {
            case TYPE_MESSAGE:
                setTypeMessage(helper, (List<Message>) model.getData());
                break;
            case TYPE_NEWS:
                setTypeNews(helper, (List<News>) model.getData());
                break;
            case TYPE_LUNBO:
                setTypeVideo(helper, (List<Video>) model.getData());
                break;
            case TYPE_VIEW:
                setTypeViewPorint(helper, (List<ViewPorint>) model.getData());
                break;
        }

    }

    private void setTypeMessage(BaseViewHolder helper, final List<Message> item) {
        RecyclerView item_list = helper.getView(R.id.rv_item_list);
        //设置布局管理器
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(MyApplication.getInstance());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        item_list.setLayoutManager(linearLayoutManager);
        IndexMessageAdapter adapter = new IndexMessageAdapter(item, MyApplication.getInstance());
        item_list.setAdapter(adapter);
        adapter.notifyDataSetChanged();
    }

    private void setTypeNews(BaseViewHolder helper, final List<News> item) {
        RecyclerView item_list = helper.getView(R.id.rv_item_list);
        //设置布局管理器
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(MyApplication.getInstance());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        item_list.setLayoutManager(linearLayoutManager);
        NewsListQuickAdapter adapter = new NewsListQuickAdapter(item, MyApplication.getInstance());
        item_list.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter baseQuickAdapter, View view, int position) {
                Intent intent = new Intent(context, ArticleDetailActivity.class);
                intent.putExtra(DATA_STRING, FastjsonUtils.toJSONString(item.get(position)));
                context.startActivity(intent);
            }
        });
    }

    private void setTypeVideo(BaseViewHolder helper, final List<Video> item) {

        RollPagerView rollPagerView = (RollPagerView) helper.getView(R.id.pv_lunbo);
        rollPagerView.setHintView(new ColorPointHintView(MyApplication.getInstance(), Color.GRAY, Color.WHITE));
        rollPagerView.getLayoutParams().height = UiUtil.getScreenWidth() * 12 / 25;
        IndexVideoLunBoAdapter adapter = new IndexVideoLunBoAdapter(item, MyApplication.getInstance(), rollPagerView);
        rollPagerView.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        rollPagerView.setOnItemClickListener(new com.jude.rollviewpager.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Intent videoIntent = new Intent(context, VideoDetailActivity.class);
                videoIntent.putExtra(VIDEO, FastjsonUtils.toJSONString(item.get(position)));
                context.startActivity(videoIntent);
            }
        });
    }

    private void setTypeViewPorint(BaseViewHolder helper, final List<ViewPorint> item) {
        RecyclerView item_list = helper.getView(R.id.rv_item_list);
        //设置布局管理器
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(MyApplication.getInstance());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        item_list.setLayoutManager(linearLayoutManager);
        ViewPorintMultQuickAdapter adapter = new ViewPorintMultQuickAdapter(item, MyApplication.getInstance());
        item_list.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter baseQuickAdapter, View view, int position) {
                Intent intent = new Intent(context, ViewPorintDetailActivity.class);
                intent.putExtra(DATA_STRING, FastjsonUtils.toJSONString(item.get(position)));
                context.startActivity(intent);
            }
        });
    }
}
